package net.homelinux.chaoswg.io.renga.krypto.animations.Rotate;

import net.homelinux.chaoswg.io.renga.core.animations.Animation;
import net.homelinux.chaoswg.io.renga.core.animations.State;
import net.homelinux.chaoswg.io.renga.krypto.animations.Rotate.RotateState;
import net.homelinux.chaoswg.io.renga.krypto.animations.Rotate.RotateStateChanger;

public class RotateAnimation extends Animation {
    RotateState initialState;
    RotateStateChanger RotateChanger;
    
    public static final int LEFT = 0;
    public static final int RIGHT = 1;

    public RotateAnimation (int direction) {
        initialize(new RotateState (), direction);
    }

    public RotateAnimation (RotateState initialState, int direction) {
        initialize(initialState, direction);
    }

    private void initialize(State initialState, int direction) throws IllegalArgumentException {
        if (!(initialState instanceof RotateState)) {
            throw new IllegalArgumentException ();
        }
        this.initialState = ((RotateState)initialState);
        this.RotateChanger = new RotateStateChanger (this.initialState, direction);
    }

    @Override
    public synchronized void start() {
        Thread t = new Thread (RotateChanger);
        t.setDaemon(true);
        t.start();
        
        try {
            t.join();
        } catch (InterruptedException iexcp){ 
            iexcp.printStackTrace();
        }
        RotateChanger.close ();
        finished = true;
        setChanged();
        notifyObservers();
    }

    @Override
    public void setOperand(int i, Object operand) {
        initialState.setOperand(i, operand);
    }
}
